package org.tekmob.remindpal.controller;

import java.util.List;

import org.tekmob.remindpal.R;
import org.tekmob.remindpal.model.Reminder;
import org.tekmob.remindpal.model.ReminderDAO;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
/**
 * This activity will be called when "Get Started" Button
 * in the flash screen clicked
 * 
 * Show all of Reminder Name in SQLite
 * 
 * @author Annisa
 *
 */
public class ShowReminder extends Activity {

	/*
	 * For dummy data
	 * @Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);	
		setContentView(R.layout.activity_show_reminder);
		ListView list = (ListView) findViewById(R.id.reminder);
		String[] reminder = { "Bangun Tidur", "Nyuci Baju", "Rapat Tekmob Woy"};
		ArrayAdapter<String> adapt = new ArrayAdapter<String>(this,
				R.layout.my_remind_pal, reminder);
		list.setAdapter(adapt); 

	}
	private ReminderDAO dao;
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);	

		// Create DAO object
		dao = new ReminderDAO(this);

		// Set the list adapter and get TODOs list via DAO
		setListAdapter(new ListAdapter(this, dao.getReminder()));
	}

	 */

	public List<Reminder> reminder;
	public ReminderDAO dao;
	private final int remove = R.id.delete;
	private final int upd = R.id.update;

	ListView listview;
	ArrayAdapter<String> reminderArrayAdapter;
	String[] nameReminder;
	TextView title;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_view);

		title = (TextView)findViewById(R.id.ti);
		title.setText("Your Reminder List");

		//Initiate database
		dao = new ReminderDAO(this);
		reminder = dao.getReminder();
		//get list of reminder name
		nameReminder = new String[reminder.size()];
		for (int i = 0; i < reminder.size(); i++){
			nameReminder[i] = (dao.getAReminder(reminder.get(i).getId())).get(0).getName();
		}

		reminderArrayAdapter = new ArrayAdapter<String>(ShowReminder.this, R.layout.my_remind_pal, nameReminder);
		listview = (ListView) findViewById(R.id.list_view);
		listview.setItemsCanFocus(false);
		listview.setAdapter(reminderArrayAdapter);

		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int pos,
					long id) {
				// TODO Auto-generated method stub
				String item = parent.getItemAtPosition(pos).toString();
				Intent i = new Intent(ShowReminder.this, DetailReminder.class);
				//i.putExtra("NAME", item);
				i.putExtra("ID", (int)dao.getReminderId(item));
				startActivity(i);

			}

		});
		registerForContextMenu(listview);

	}
	
	/*
	 * Initiate Option Menu
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.list_reminder, menu);
		return true;

	}
	
	/*
	 * Option Menu Handler
	 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		switch(item.getItemId()) {
		case R.id.new_reminder:
			//click on create item
			Intent newReminder = new Intent(getApplicationContext(),NewReminder.class);
			startActivity(newReminder);
			break; 
		case R.id.show:
			//click on show item
			Intent showReminder = new Intent(getApplicationContext() , ShowReminder.class);
			startActivity(showReminder);
			break;
		case R.id.send:
			//click on send item
			Intent sendReminder = new Intent(getApplicationContext(),SendReminder.class);
			startActivity(sendReminder);
			break;
		}
		return true;
	}


	/*
	 * Initiate Context Menu when Item LongClick
	 * @see android.app.Activity#onCreateContextMenu(android.view.ContextMenu, android.view.View, android.view.ContextMenu.ContextMenuInfo)
	 */
	@Override
	public void onCreateContextMenu(ContextMenu m,	View v, ContextMenuInfo i) {
		super.onCreateContextMenu(m, v, i);
		m.setHeaderTitle("Option");
		m.add(0, this.remove, 0, "Delete");
		m.add(0, this.upd, 0, "Update");
	}

	/*
	 * Context Menu Handler
	 * @see android.app.Activity#onContextItemSelected(android.view.MenuItem)
	 */
	@Override
	public boolean onContextItemSelected(MenuItem item) {

		AdapterView.AdapterContextMenuInfo info = null;
		info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
		ListView list = (ListView) findViewById(R.id.list_view);
		final String name = (String) list.getAdapter().getItem(info.position);

		switch (item.getItemId()) {
		case R.id.delete:
			//TODO: hapus Paket acara dari database

			new AlertDialog.Builder(this).setMessage("This reminder will be deleted") 
			.setTitle("Delete")
			.setPositiveButton("OK",  
					new DialogInterface.OnClickListener() {  
				@Override
				public void onClick(DialogInterface dialog, int whichButton){
					int id = dao.getReminderId(name);
					Intent intent = new Intent(ShowReminder.this, AlarmReceiverActivity.class);
					PendingIntent pendingIntent = PendingIntent.getActivity(ShowReminder.this,
							id, intent, PendingIntent.FLAG_CANCEL_CURRENT);
					AlarmManager am = 
							(AlarmManager) ShowReminder.this.getSystemService(Activity.ALARM_SERVICE);
					am.cancel(pendingIntent);
					
					dao.deleteReminder(name);
					Intent i = new Intent(ShowReminder.this, ShowReminder.class);
					startActivity(i);
				}  
			})    
			.setNegativeButton("Cancel",  
					new DialogInterface.OnClickListener() {  
				@Override
				public void onClick(DialogInterface dialog, int whichButton){
					dialog.cancel();

				}  
			})  
			.show(); 

			return true;		
		case R.id.update:
			Intent intent = new Intent(ShowReminder.this, UpdateReminder.class);
			//intent.putExtra("NAME", name);
			intent.putExtra("ID", (int)dao.getReminderId(name));
			startActivity(intent);
			return true;
		default:
			return super.onContextItemSelected(item);
		}
	}
}
